<?php

/**
 * jwGallery form.
 *
 * @package    ##PROJECT_NAME##
 * @subpackage form
 * @author     ##AUTHOR_NAME##
 * @version    SVN: $Id: sfPropelFormTemplate.php 10377 2008-07-21 07:10:32Z dwhittle $
 */
class jwGalleryForm extends BasejwGalleryForm
{
  public function configure()
  {
    $this->setWidgets(array(
      'id'          => new sfWidgetFormInputHidden(),
      'path'        => new sfWidgetFormInputHidden(),
      'name'        => new sfWidgetFormInput(),
      'description' => new sfWidgetFormTextarea(),
      'updated_at'  => new sfWidgetFormInputHIdden(),
      'updated_by'  => new sfWidgetFormInputHIdden(),
      'pictures_man'    => new jwWidgetGalleryPictures(),
    ));

    $this->setValidators(array(
      'id'          => new sfValidatorPropelChoice(array('model' => 'jwGallery', 'column' => 'id', 'required' => false)),
      'path'        => new sfValidatorString(array('max_length' => 50, 'required' => false)),
      'name'        => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'description' => new sfValidatorString(array('required' => false)),
      'updated_at'  => new sfValidatorDateTime(array('required' => false)),
      'updated_by'  => new sfValidatorInteger(array('required' => false)),
      'pictures_man'    => new sfValidatorPass(),
    ));

	$pictures_form = new jwPictureManagerForm();
    
	$a = new jwGalleryPictureForm();
	
    $c = new Criteria();
    $c->addAscendingOrderByColumn( jwGalleryPicturePeer::ORDER );
    
	$pictures = $this->getObject()->getjwGalleryPictures( $c );
    $this->getObject()->clearjwGalleryPictures();
	
	foreach( $pictures as $picture )
	{
	  $pic = new jwGalleryPictureForm( $picture );
	  $pictures_form->embedForm( $picture->getId(), $pic );
	}
	
	$this->embedForm( 'pictures', $pictures_form );
	
    $this->widgetSchema->setNameFormat('jw_gallery[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);
    $this->getObject()->forceUpdate();    
  }
  
  public function bind( array $taintedValues = null, array $taintedFiles = null )
  {
	jwGalleryForm::updateFromRequest( $this, &$taintedValues );

    parent::bind( $taintedValues, $taintedFiles );
  }
  
  public static function updateFromRequest( sfForm &$form, array &$taintedValues = null )
  {
    $form->embeddedForms['pictures']->embeddedForms = array();

    if ( isset( $taintedValues['pictures'] ) )
    {
      foreach( $taintedValues['pictures'] as $key => $taintedPictureValues )
      {		
		$id =  $taintedPictureValues['id'];
		$image = jwGalleryPicturePeer::retrieveByPK( $id );
		if ( !$image )
		{
		  $image = new jwGalleryPicture();
		}
		  
		$image->setjwGallery( $form->getObject() );
		

		if ( !$form->getObject()->isNew() )
		{
		  $taintedValues['pictures'][$key]['gallery_id'] = $form->getObject()->getId();
		}

		$tmp = new jwGalleryPictureForm( $image );
		$form->embeddedForms['pictures']->embedForm( $key, $tmp );
      }
	  $form->embedForm( 'pictures', $form->embeddedForms['pictures'] );
    }	
  }
}
